
package cribbage.client;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

import com.samskivert.swing.Controller;
import com.samskivert.swing.Label;
import com.samskivert.swing.LabelStyleConstants;
import com.sixlegs.client.CenteredLabelSprite;
import com.threerings.media.image.BufferedMirage;
import com.threerings.media.image.Mirage;
import com.threerings.parlor.card.client.CardPanel;
import com.threerings.parlor.card.client.CardSprite;
import com.threerings.parlor.card.data.Card;
import com.threerings.parlor.card.data.Hand;
import com.threerings.toybox.util.ToyBoxContext;
import com.threerings.util.Name;

import cribbage.data.CribPredicate;
import cribbage.data.CribbageCodes;

public class HandPanel extends CardPanel {

   private static final Font FONT = new Font("Arial", Font.BOLD, 16);
   public static final Color feltColor = DEFAULT_BACKGROUND;

   private Hand hand;

   private int bigWidth, bigHeight;

   private int smallWidth, smallHeight;

   private BufferedImage bigCards;

   private BufferedImage smallCards;

   CenteredLabelSprite panelLabel;

   Label text;

   Name name;
   
   Color felt;

   private int xLoc, yLoc;

   private boolean offsetFromTop;

   static private CribPredicate cribPredicate;

   public HandPanel (ToyBoxContext ctx, boolean atTop, Color f) {
      super(ctx.getFrameManager());

      hand = null;
      offsetFromTop = atTop;
      felt = f;
      cribPredicate = new CribPredicate(this);

      bigCards = ctx.loadImage("media/cards.png");
      smallCards = ctx.loadImage("media/cards_micro.png");
      bigWidth = bigCards.getWidth() / 14;
      bigHeight = bigCards.getHeight() / 4;
      smallWidth = smallCards.getWidth() / 14;
      smallHeight = smallCards.getHeight() / 4;

      _cardWidth = bigWidth;

      _handSelectionMode = CardPanel.MULTIPLE;
      this.setHandSelectionPredicate(cribPredicate);

      text = new Label("", LabelStyleConstants.OUTLINE, Color.green, Color.black, FONT);
      panelLabel = new CenteredLabelSprite(text);
      panelLabel.setLocation(0, 0);

      addSprite(panelLabel);

      setHandSpacing(bigWidth / 2);
   }

   public void setHand (Hand h) {
      hand = h;
      setHand(hand, 500);
   }

   public void setHand (int size) {
      if (size == 0) {
         hand = null;
         clearHand();
      }
      else
         setHand(size, 500);
   }

   @Override
   public void selectHandSprite (final CardSprite sprite) {
      super.selectHandSprite(sprite);

      Controller.postAction(this, CribbageCodes.SELECT, sprite.getCard());
   }

   @Override
   public void deselectHandSprite (final CardSprite sprite) {
      super.deselectHandSprite(sprite);

      Controller.postAction(this, CribbageCodes.DESELECT, sprite.getCard());
   }

   @Override
   public void doLayout () {
      System.out.println("Doing layout with hand of size "+(hand == null ? 0 : hand.size()));
      xLoc = getWidth() / 2;
      if (offsetFromTop)
         yLoc = (int) (getHeight() * 0.1);
      else
         yLoc = (int) (getHeight() * 0.9) - bigHeight;

      setHandLocation(xLoc, yLoc);
      adjustHand(1, true);

      text.setText(name + " panel");
      text.layout(this);
      panelLabel.updateBounds();
      panelLabel.setLocation(getWidth() / 2, getHeight() / 2);
   }

   @Override
   protected void paintBehind (Graphics2D gfx, Rectangle dirtyRect) {
      gfx.setColor(felt);
      gfx.fill(dirtyRect);
//      super.paintBehind(gfx, dirtyRect);
   }

   /**
    * 
    * 
    * @return Returns the cardHeight.
    */
   public int getBigHeight () {
      return bigHeight;
   }

   /**
    * 
    * 
    * @return Returns the _cardWidth.
    */
   public int getBigWidth () {
      return bigWidth;
   }

   public int getHandWidth () {
      return bigWidth + (_handSprites.size() - 1) * _handSpacing;
   }

   public Hand getHand () {
      return hand;
   }

   @Override
   public Mirage getCardBackImage () {
      return new BufferedMirage(bigCards.getSubimage(13 * bigWidth, 0, bigWidth, bigHeight));
   }

   @Override
   public Mirage getCardImage (Card card) {
      return new BufferedMirage(bigCards.getSubimage((card.getNumber() - 2) * bigWidth, card
            .getSuit()
            * bigHeight, bigWidth, bigHeight));
   }

   @Override
   public Mirage getMicroCardBackImage () {
      return new BufferedMirage(smallCards.getSubimage(0, 13 * smallWidth, smallWidth, smallHeight));
   }

   @Override
   public Mirage getMicroCardImage (Card card) {
      return new BufferedMirage(smallCards.getSubimage((card.getNumber() - 2) * smallWidth, card
            .getSuit()
            * smallHeight, smallWidth, smallHeight));
   }

   public void playCards () {
      flyFromHand(getSelectedHandSprites(), new Point(getWidth() / 2, 0), 100L, 0f);
   }
}
